package com.bigshark.api.controller;

import com.bigshark.api.annotation.Login;
import com.bigshark.api.annotation.LoginUser;
import com.bigshark.api.form.CVForm;
import com.bigshark.api.form.CVTextForm;
import com.bigshark.api.model.Conventions;
import com.bigshark.api.model.User;
import com.bigshark.api.service.ConventionsService;
import com.bigshark.common.utills.PageUtils;
import com.bigshark.common.utills.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


@RestController
@RequestMapping("/api/cv")
public class ConventionsController {
    @Autowired
    private ConventionsService cvService;

    @GetMapping("/")
    @ApiOperation(value = "漫展", notes = "漫展列表", tags = "漫展接口", httpMethod = "GET")
    public R queryCVList(CVForm cvForm) {
        PageUtils page = cvService.selectList(cvForm);
        return R.ok().put("data", page);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "漫展詳情", notes = "漫展詳情", tags = "漫展接口", httpMethod = "GET")
    public R queryCvById(@PathVariable Long id) {
        Conventions conventions = cvService.getById(id);
        return R.ok().put("data", conventions);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除漫展信息", notes = "删除漫展信息", tags = "漫展接口", httpMethod = "DELETE")
    public R deleteById(@PathVariable Long id) {
        if (!cvService.removeById(id)) {
            return R.error("删除失败");
        }
        return R.ok();
    }

    @Login
    @PutMapping("/{id}")
    @ApiOperation(value = "修改漫展信息", notes = "修改漫展信息", tags = "漫展接口", httpMethod = "PUT")
    public R upData(@PathVariable Long id, @RequestBody CVTextForm cvForm,@ApiIgnore @LoginUser User user) {
        Conventions con = cvService.getById(id);
        Assert.notNull(con,"修改文章出错");
        if(con.getUserId() != user.getId()){
            return R.error("您没有权限修改");
        }
        con = generateCv(cvForm);
        if (!cvService.saveOrUpdate(con)) {
            return R.error("修改失败");
        }
        return R.ok();
    }

    @Login
    @PostMapping("/")
    @ApiOperation(value = "添加漫展信息", notes = "添加漫展信息", tags = "漫展接口", httpMethod = "POST")
    public R createCv(@RequestBody CVTextForm cvForm,@ApiIgnore @LoginUser User user){
        Conventions con = generateCv(cvForm);
        con.setUserId(user.getId());
        if(!cvService.save(con))
        {
           return R.error("添加失败");
        }
        return R.ok();
    }

    public Conventions generateCv(CVTextForm cvForm){
        Conventions con = new Conventions();
        con.setCvPrice(cvForm.getPrice());
        con.setAddress(cvForm.getAddress());
        con.setCvCoverUrl(cvForm.getCoverUrl());
        con.setCvIntroduction(cvForm.getIntroduction());
        con.setCvDetail(cvForm.getDetail());
        con.setCvShopUrl(cvForm.getShopUrl());
        con.setStartTime(cvForm.getStartTime());
        con.setEndTime(cvForm.getEndTime());
        con.setStatue(cvForm.getStatue());
        con.setCvTitle(cvForm.getTitle());
        return con;
    }

}
